<template>
  <div class="current-strategy">
    <el-card class="box-card">
      <div class="title-phone">
        <div class="top-phone">昵称 - {{nickname}}</div>
        <div class="top-btn">
          <el-button type="primary" @click="goRefresh">刷新</el-button>
          <el-button @click="goBack">返回</el-button>
        </div>
      </div>
      <div class="item">
        <div class="item-head"><span>锁屏/解锁</span></div>
        <div class="item-body">
          <span class="item-body-color">{{lockStatus | lockFmt}}</span>
        </div>
      </div>
      <div class="item">
        <div class="item-head"><span>是否在线</span></div>
        <div class="item-body">
          <span class="item-body-color">{{onlineStatus == 1 ? '离线' : '在线'}}</span>
        </div>
      </div>
      <div class="item">
        <div class="item-head"><span>可用时段</span></div>
        <div class="item-body">
          <cvue-table :option="tableOption" :data="tableData" ref="tbl1">
            <!-- 可用时段 -->
            <template slot-scope="scope" slot="time">
              <span v-for="item in scope.row.time" :key="item" class="time-box">{{item}}</span>
            </template>
          </cvue-table>
        </div>
      </div>
      <div class="item-box">
        <div class="item-head"><span>应用管理</span></div>
        <div class="item-body">
          <div class="app-box">
            <div class="app-left">自由使用</div>
            <div class="app-right">{{freeStr}}</div>
          </div>
          <div class="app-box">
            <div class="app-left">禁止使用</div>
            <div class="app-right">{{disabledStr}}</div>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>
<script>
import cvueTable from '@/components/cvue-table'
import cvueDialog from '@/components/cvue-dialog'
import { baseUrl } from '@/config/config.js'
export default {
  name: 'currentStrategy',
  components: {
    cvueTable,
    cvueDialog
  },
  data() {
    return {
      showWord: '',
      hasPermission: false,
      controlledId: '',
      vipstate: '',
      nickname: '',
      lockStatus: '', // 锁屏状态
      onlineStatus: '', // 在线状态
      freeStr: '', // 自用使用的应用
      disabledStr: '', // 禁止使用的应用
      tableLoading: false,
      tablePage: 1,
      page: {
        total: 1, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10, // 每页显示多少条
        pageSizes: [10, 50, 100],
      },
      tableOption: {
        stripe: true,
        width: '100%',
        border: true,
        index: false,
        cloumn: [
          { prop: 'week', label: '星期', align: 'center', width: '200' },
          {
            prop: 'time',
            label: '可用时段',
            align: 'center',
            slotStatus: true,
          },
        ],
      },
      tableData: [
        { week: '星期一', time: [] },
        { week: '星期二', time: [] },
        { week: '星期三', time: [] },
        { week: '星期四', time: [] },
        { week: '星期五', time: [] },
        { week: '星期六', time: [] },
        { week: '星期天', time: [] }
      ]
    }
  },
  computed: {},
  mounted() {
    this.controlledId = this.$route.query.id
    this.nickname = this.$route.query.nickname
    this.vipstate = this.$route.query.vipstate
    let permission = sessionStorage.getItem('permission')
    if (permission) {
      permission = JSON.parse(permission)
      permission.forEach((p) => {
        if (p.code == '12020' && p.permissions.indexOf('edit') != -1) {
          this.hasPermission = true
        }
      })
    }
    this.handleList() // 获取列表详情
  },
  filters: {
    lockFmt (val) {
      let obj = ''
      //  0-长期解锁 1-长期锁定 2-临时解锁 3-临时锁定 4-按时段守护
      if (val == 0) {
        obj = '长期解锁'
      } else if (val == 1) {
        obj = '长期锁定'
      } else if (val == 2) {
        obj = '临时解锁'
      } else if (val == 3) {
        obj = '临时锁定'
      } else if (val == 4) {
        obj = '按时段守护'
      }
      return obj
    }
  },
  methods: {
    // 列表接口
    handleList() {
      this.tableLoading = true
      this.timer = []
      this.tableData2 = []
      var params = {
        controlledId: this.controlledId,
      }
      this.$store
        .dispatch('commonUser/GetPolicy', params)
        .then((res) => {
          console.log(res)
          if (res.code == 200) {
            this.showWord = 'hasData' // 如果有正确的数据返回，这样会从空白到显示，否则会有文字先出现再改变
            this.setTimeControl(res.data.timer)
            this.freeStr = this.appInit(res.data.free)
            this.disabledStr = this.appInit(res.data.disabled)
            this.lockStatus = res.data.lockStatus
            this.onlineStatus = res.data.onlineStatus
          } else {
            this.showWord = 'noData'
            this.$message({
              message: res.msg,
              duration: 2000,
              type: 'error',
            })
          }
          this.tableLoading = false
        })
        .catch((err) => {
          console.log(err)
          this.tableLoading = false
        })
    },
    // 禁用时段设置
    setTimeControl(timer) {
      console.log('setTimeControl', timer)
      this.tableData = [
        { week: '星期一', time: [] },
        { week: '星期二', time: [] },
        { week: '星期三', time: [] },
        { week: '星期四', time: [] },
        { week: '星期五', time: [] },
        { week: '星期六', time: [] },
        { week: '星期日', time: [] }
      ]
      if (timer == null) {
        for (let j = 0; j < this.tableData.length; j++) {
          this.tableData[j].time.push('00:00-24:00')
          // this.tableData[j].message.push('00:00-24:00')
        }
        return
      }
      // 循环生成可用时段
      timer.forEach((item) => {
        // let repeat = item.repeat.split('').reverse().join('')
        let repeat = item.repeat.split('').join('')
        console.log(repeat)
        for (let i = 0; i < repeat.length; i++) {
          console.log(repeat, repeat[i])
          if (repeat[i] == 1) {
            // if (item.timerType == 0) {
              // this.tableData[i].time += item.beginTime + '-' + item.endTime + '&nbsp;&nbsp;'
              this.tableData[i].time.push(item.beginTime + '-' + item.endTime)
            // }
          }
        }
      })
      // 得到的数据再次循环，判断无数据的情况
      for (let k = 0; k < this.tableData.length; k++) {
        if (this.tableData[k].time == '') {
          this.tableData[k].time = '-'
        }
      }
    },
    appInit(arr){
      console.log(arr);
      if (arr.length == 0) {
        return "无"
      } else {
        let obj = ''
        for (var i = 0; i < arr.length; i++) {
          obj = obj + arr[i] + '、'
        }
        obj = obj.substring(0, obj.length - 1)
        return obj
      }
    },
    goRefresh() {
      // this.controlledId = this.$route.query.id
      this.handleList() // 获取列表详情
    },
    goBack() {
      // this.closeTagTo('/user/open-list', true, {})
      var path = '/user/open-list'
      var status = true
      var query = {
        userId: this.$route.query.userId,
        phone: this.$route.query.phone,
        vipstate: this.$route.query.vipstate
      }
      this.closeTagTo(path, status, query)
    },
  },
}
</script>
<style lang="scss">
.current-strategy {
  padding: 0px !important;
  height: auto !important;
  // .item{
  //     margin-bottom: 20px;
  // }
  .title-phone {
    font-size: 18px;
    font-weight: 600;
    margin-bottom: 15px;
    height: 40px;
    .top-phone {
      // height: 50px;
      // line-height: 50px;
      float: left;
    }
    .top-btn {
      // height: 50px;
      // line-height: 50px;
      float: right;
    }
  }
  .item-head {
    height: 20px;
    position: relative;
    border-bottom: 1px dashed #ccc;
    margin-bottom: 12px;
    // margin-top: -10px;
  }
  .item-head span {
    display: inline-block;
    position: absolute;
    left: 0;
    bottom: -10px;
    background: #fff;
    font-size: 16px;
    font-weight: 600;
    padding: 0 10px 0 16px;
    box-sizing: border-box;
    height: 18px;
    // text-indent: 15px;
    line-height: 18px;
  }
  .item-body {
    padding: 20px 16px;
    font-size: 14px;
    .item-body-in {
      padding-left: 30px;
      padding-top: 5px;
    }
    .item-body-color{
      color: #333333;
    }
    .app-box{
      width: 100%;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      border: 1px solid #EBEEF5;
      font-size: 12px;
      color: #333;
      .app-left{
        width: 200px;
        text-align: center;
        // border-right: 1px solid #EBEEF5;
        padding: 12px 15px;
        box-sizing: border-box;
        font-weight: 600;
      }
      .app-right{
        flex: 1;
        border-left: 1px solid #EBEEF5;
        text-align: left;
        padding: 12px 24px;
        box-sizing: border-box;
      }
    }
    .app-box:first-child{
      background: #FAFAFA;
      margin-bottom: -1px;
    }
  }
  .time-box {
    display: inline-block;
    margin: 0 5px;
  }
  .item-body-limit {
    margin-bottom: 10px;
  }
  .group-name {
    cursor: pointer;
    color: #409eff;
  }
  .item-body-limit-second {
    margin-top: 20px;
  }
  .el-card__body {
    padding: 24px 40px;
  }
  .btn-wrap {
    text-align: center;
  }
}
</style>
<style>
/* .current-strategy .ss{
  font-size: 14px!important;
} */
</style>
